package com.sjm.suchbear.minibookmall.fragment.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjm.suchbear.minibookmall.R;
import com.sjm.suchbear.minibookmall.model.BookModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by GST on 2018/5/7.
 */

public class BookListAdapter extends BaseAdapter {

    private ViewHolder viewHolder;
    private Context context;
    private List<BookModel> books = new ArrayList<>();

    public BookListAdapter(Context context, List<BookModel> books) {
        this.context = context;
        this.books = books;
    }

    @Override
    public int getCount() {
        if (books != null) {
            return books.size();
        } else {
            return 0;
        }
    }


    @Override
    public Object getItem(int i) {
        return books.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_show, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        BookModel bookModel = books.get(i);
        viewHolder.tvName.setText(bookModel.getBookname());
        viewHolder.tvSec.setText("作者：".concat(bookModel.getBookauth()));
        viewHolder.tvPrice.setText("¥".concat(bookModel.getBookprice()));
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.cb_choose)
        CheckBox cbChoose;
        @BindView(R.id.rl_choose)
        RelativeLayout rlChoose;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_sec)
        TextView tvSec;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.ll_all)
        LinearLayout llAll;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
